
package utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @created May 15, 2004 
 * Allows to manipulate object and espacially read an object from a file
 * Although the features provided by this class and ObjectWriter seem "reduit",
 * it has been chosen to seperate ObjectReader and ObjectWriter in order to ensure 
 * flexibility in case we have to add other services.
 * @author Equipe SimPack 2004
 * 
 */
public class ObjectReader {

    // for log purposes
    private static final Logger fLogger = Logger.getLogger(ObjectReader.class.getPackage().getName());

    /**
     * Read an object from a file
     * @param fileName filename of the file in which we'll write the object
     * @return the read object
     */
    public static Object readObject(String fileName){
	//deserialize the fileName file
	
	//declared here only to ensure visibilty in finally clause
	ObjectInput input = null;
	try{
	    //use buffering
	    InputStream file = new FileInputStream( fileName );
	    InputStream buffer = new BufferedInputStream( file );
	    input = new ObjectInputStream ( buffer );
		  
	    return input.readObject();
	}
	catch(IOException ex){
	    fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
	} catch (ClassNotFoundException e) {
			
	    fLogger.log(Level.SEVERE, "Unexpected class found upon input.", e);
	    e.printStackTrace();
	}
		
	finally{
	    try {
		if ( input != null ) {
		    //close "input" and its underlying streams
		    input.close();
		}
	    }
	    catch (IOException ex){
		fLogger.log(Level.SEVERE, "Cannot close input stream (filename : " + fileName + ").", ex);
	    }
			
	}
	return null;
    }

    public static void main(String[] args) {
    }
}
